package com.mmy.controller.visa;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
*类的介绍：签证所需材料信息 
*
*@author 	hanxl
*@date		2020-07-23
*@version		1.0
*/
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.sys.TSysUser;
import com.mmy.domain.visa.TVisaStuff;
import com.mmy.service.impl.visa.TVisaBaseServiceImpl;
import com.mmy.service.impl.visa.TVisaStuffServiceImpl;

@Controller
public class TVisaStuffController {
	@Autowired
	TVisaStuffServiceImpl tVisaStuffServiceImpl;
	@Autowired
	TVisaBaseServiceImpl tVisaBaseServiceImpl;

	/* 显示签证基本信息首页 */
	@RequestMapping(value = "/tocreatevisastuff")
	public String FindAll(HttpServletRequest request, Model mVisaStuff) {

		String iVisaID = request.getParameter("iVisaID");
		mVisaStuff.addAttribute("iVisaID", iVisaID);
		return "webbackground/visamanagement/createvisastuff";
	}

	/*
	 * 输入创建信息提交
	 */
	@RequestMapping(value = "/savecreatevisastuff")
	@ResponseBody
	public String SaveCreateVisaStuff(@RequestParam("stuff") String strJsonStuff, HttpSession session,
			@RequestParam("visaID") String strJsonVisaID) throws ParseException {
		String strVisaID = strJsonVisaID.replace("\"", "");
		int iVisaID = Integer.parseInt(strVisaID);
		// 手动反序列化

		List<TVisaStuff> lstStuff = doJsonToVisaStuff(strJsonStuff);
		/* 创建线路基本信息 */
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		/* 循环保存创建线路信息 */
		for (int i = 0; i < lstStuff.size(); i++) {
			/* 获得最大材料编码 */
			Object iStuffID = tVisaStuffServiceImpl.getMaxStuffID();
			/*
			 * 判断数据库是否存在菜单id 如果存在在最大值基础上加一 如果不存在把最大值置为一
			 */
			TVisaStuff tVisaStuff = lstStuff.get(i);
			if (iStuffID == null) {
				tVisaStuff.setStuffID(100000000);
			} else {
				tVisaStuff.setStuffID(Integer.parseInt(iStuffID.toString()) + 1);
			}
			tVisaStuff.setVisaID(iVisaID);
			tVisaStuff.setCreateDate(new Date());

			tVisaStuff.setCreator(tSysUser.getUserName());
			tVisaStuffServiceImpl.save(tVisaStuff.getStuffID());
			tVisaStuffServiceImpl.save(tVisaStuff);
		}
		return "redirect:visaindex";
	}

	/**
	 * 手动反序列化行程信息
	 *
	 * @param strJson 要反序列化的字符串
	 * @return List<TLineTrip> 反序列化后的实体类list
	 * @author hanxl,dingxd
	 * @date 2020/9/10
	 */
	private List<TVisaStuff> doJsonToVisaStuff(String strJson) {
		// 格式化字符串
		// 去掉首尾标识
		String strStuff = strJson.replace("[{", "").replace("}]", "").replace(",{", "");
		// 拆分字符串
		String[] arrStuff = strStuff.split("}");
		// 声明List对象
		List<TVisaStuff> lstStuff = new ArrayList<TVisaStuff>();
		// 拆分子字符串
		for (int i = 0; i < arrStuff.length; i++) {
			String[] arrStuffSon = arrStuff[i].replace("\"", "").split(",");
			// 获得有效值
			for (int j = 0; j < arrStuffSon.length; j++) {
				arrStuffSon[j] = arrStuffSon[j].substring(arrStuffSon[j].indexOf(":") + 1, arrStuffSon[j].length());
			}
			// 一对一映射实体类赋值
			TVisaStuff tStuff = new TVisaStuff();
			tStuff.setNo((byte) Integer.parseInt(arrStuffSon[0]));

			tStuff.setStuffName(arrStuffSon[1]);
			tStuff.setNumber((byte) Integer.parseInt(arrStuffSon[2]));

			tStuff.setMemo(arrStuffSon[3]);
			lstStuff.add(tStuff);
		}
		return lstStuff;
	}

	/* 进入修改材料页面 */
	@RequestMapping(value = "/toupdatevisastuff")
	public String toUpdateVisaStuff(HttpServletRequest request, Model model) {
		int iVisaID = Integer.parseInt(request.getParameter("iVisaID"));
		/* 查询该签证的材料表 */
		List<TVisaStuff> db_TVisaStuff = tVisaStuffServiceImpl.findByVisaID(iVisaID);
		model.addAttribute("newTVisaStuff", db_TVisaStuff);
		model.addAttribute("VisaID", iVisaID);
		return "webbackground/visamanagement/updatevisastuff";
	}

	/*
	 * 输入修改信息提交
	 */
	@RequestMapping(value = "/saveupdatevisastuff")
	@ResponseBody
	public String SaveCreateSurrouding(@RequestParam(value = "VisaID") String strJsonVisaID,
			@RequestParam("stuff") String strJsonStuff, HttpSession session) throws ParseException {

		// 手动反序列化
		String strVisaID = strJsonVisaID.replace("\"", "");
		int iVisaID = Integer.parseInt(strVisaID);
		List<TVisaStuff> lstStuff = doJsonToVisaStuff(strJsonStuff);
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		/* 循环保存修改的签证材料信息 */
		/* 第一步删除该VisaID的行程信息 */
		tVisaStuffServiceImpl.deleteByVisaID(iVisaID);
		/* 第二步重新创建行程信息 */
		for (int i = 0; i < lstStuff.size(); i++) {
			/* 获得最大材料编码 */
			Object iStuffID = tVisaStuffServiceImpl.getMaxStuffID();
			/*
			 * 判断数据库是否存在菜单id 如果存在在最大值基础上加一 如果不存在把最大值置为一
			 */
			TVisaStuff tVisaStuff = lstStuff.get(i);
			if (iStuffID == null) {
				tVisaStuff.setStuffID(100000000);
			} else {
				tVisaStuff.setStuffID(Integer.parseInt(iStuffID.toString()) + 1);
			}
			tVisaStuff.setVisaID(iVisaID);
			tVisaStuff.setCreateDate(new Date());

			tVisaStuff.setCreator(tSysUser.getUserName());
			tVisaStuffServiceImpl.save(tVisaStuff.getStuffID());
			tVisaStuffServiceImpl.save(tVisaStuff);
		}
		return "webbackground/visamanagement/visainfo?iVisaID=" + iVisaID;
	}

}
